package com.bw.liuyushan.liuyushan20210904.MVP.Activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bw.liuyushan.liuyushan20210904.Adapter.FragmentAdapter;
import com.bw.liuyushan.liuyushan20210904.MVP.Contrack.IContrackTest;
import com.bw.liuyushan.liuyushan20210904.MVP.Fragment.Fragment1;
import com.bw.liuyushan.liuyushan20210904.MVP.Fragment.Fragment2;
import com.bw.liuyushan.liuyushan20210904.MVP.Fragment.Fragment3;
import com.bw.liuyushan.liuyushan20210904.MVP.Fragment.Fragment4;
import com.bw.liuyushan.liuyushan20210904.MVP.Fragment.Fragment5;
import com.bw.liuyushan.liuyushan20210904.MVP.Model.BaseModelTest;
import com.bw.liuyushan.liuyushan20210904.MVP.Presenter.BasePresenterTest;
import com.bw.liuyushan.liuyushan20210904.R;
import com.bw.liuyushan.mvp_lib.VIew.BaseActivity;

import java.util.ArrayList;

public class MainActivity extends BaseActivity<BasePresenterTest> implements IContrackTest.C_ViewTest {


    private androidx.viewpager.widget.ViewPager vpMain;
    private android.widget.RadioGroup rgMain;
    private android.widget.RadioButton btn1;
    private android.widget.RadioButton btn2;
    private android.widget.RadioButton btn3;
    private android.widget.RadioButton btn4;
    private android.widget.RadioButton btn5;
    ArrayList<Fragment> fragments;

    @Override
    public int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        //防止内存泄露
        mPresenter=new BasePresenterTest(new BaseModelTest(),this);
        vpMain = (ViewPager) findViewById(R.id.vp_Main);
        rgMain = (RadioGroup) findViewById(R.id.rg_Main);
        btn1 = (RadioButton) findViewById(R.id.btn1);
        btn2 = (RadioButton) findViewById(R.id.btn2);
        btn3 = (RadioButton) findViewById(R.id.btn3);
        btn4 = (RadioButton) findViewById(R.id.btn4);
        btn5 = (RadioButton) findViewById(R.id.btn5);
    }

    @Override
    public void initDate() {

        fragments = new ArrayList<>();
        fragments.add(new Fragment1());
        fragments.add(new Fragment2());
        fragments.add(new Fragment3());
        fragments.add(new Fragment4());
        fragments.add(new Fragment5());

        FragmentAdapter fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
        vpMain.setAdapter(fragmentAdapter);

        rgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.btn1:
                        vpMain.setCurrentItem(0);
                        break;
                    case R.id.btn2:
                        vpMain.setCurrentItem(1);
                        break;
                    case R.id.btn3:
                        vpMain.setCurrentItem(2);
                        break;
                    case R.id.btn4:
                        vpMain.setCurrentItem(3);
                        break;
                    case R.id.btn5:
                        vpMain.setCurrentItem(4);
                        break;
                }
            }
        });

        vpMain.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        rgMain.check(R.id.btn1);
                        break;
                    case 1:
                        rgMain.check(R.id.btn2);
                        break;
                    case 2:
                        rgMain.check(R.id.btn3);
                        break;
                    case 3:
                        rgMain.check(R.id.btn4);
                        break;
                    case 4:
                        rgMain.check(R.id.btn5);
                        break;


                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });



    }
}
